var ns, ajpush, count = 0;
var app = new Vue({
    el: '#app',
    data: {
      isselect:false,
        form: {
            username: '',
            password: '',
            device_type: '',
            device_id: '',
            openid: '',
            logintype: 1, //1-手机号登录 8-微信登录 9-qq登录
            showweixin: false,
            showqq: false,
            showweibo: false
        },
        imageurl: '../image/logo2.png'
    },
    methods: {
        get_data: function() {
            var that = this;
            ns.post({
                action: 'getrotatingimages',
                type: 3,
            }, function(succeed, data, err) {
                if (succeed) {
                    if (data.imagelist.length > 0) {
                        that.imageurl = data.imagelist[0].imageurl;
                    }
                }
            });
        },
        get_thridlogin_auth: function() {
            var that = this;
            ns.post({
                action: 'checkthridenablestatus',
            }, function(succeed, data, err) {
                if (succeed) {
                    that.form.showweixin = data.showweixin;
                    that.form.showqq = data.showqq;
                    that.form.showweibo = data.showweibo;
                }
                // if (!that.form.showweixin) {
                //     wx.isInstalled(function(ret, err) {
                //         if (ret.installed) {
                //             that.form.showweixin = true;
                //         } else {
                //             that.form.showweixin = false;
                //         }
                //     });
                // }
                // if (!that.form.showqq) {
                //     qq.installed(function(ret, err) {
                //         if (ret.status) {
                //             that.form.showqq = true;
                //         } else {
                //             that.form.showqq = false;
                //         }
                //     });
                // }
                // if (!that.form.showweibo) {
                //     weibo.isInstalled(
                //         function(ret) {
                //             if (ret.status) {
                //                 that.form.showweibo = true;
                //             } else {
                //                 that.form.showweibo = false;
                //             }
                //         }
                //     );
                // }
            });
        },
        do_register: function() {
            var that = this;
            ns.openWin('register_frm', '注册');
        },
        do_forget_password: function() {
            var that = this;
            that.$dialog.alert({
                title: '提示信息',
                message: '请联系平台或商户管理员重置登录密码',
            }).then(() => {

            })
        },
        do_choose:function(){
            var that = this;
            that.isselect = !that.isselect;
        },
        do_turnAgreement:function(){
            var that = this;
            ns.openWin('dl_agreement', '德礼APP使用协议');
        },
        do_login: function() {
            var that = this;
            if (that.form.logintype == 1) {
                if (that.form.username == '') {
                    api.toast({
                        msg: '请输入手机号码',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
                if (that.form.password == '') {
                    api.toast({
                        msg: '请输入密码',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return;
                }
                // if (!that.isselect) {
                //     api.toast({
                //         msg: '请同意并接受协议',
                //         duration: 2000,
                //         location: 'bottom'
                //     });
                //     return;
                // }
            }
            if (that.form.device_id == '' && count <= 1) {
                that.get_jiguang_register_id();
                setTimeout(function() {
                    that.do_login();
                }, 1000);
                count++;
                return;
            }
            api.removePrefs({
                key: 'uid'
            });
            api.removePrefs({
                key: 'busid'
            });
            // api.removePrefs({
            //     key: 'familyuid'
            // });
            //
            // api.removePrefs({
            //     key: 'isfirstchangepwd'
            // });
            console.log(JSON.stringify(that.form.device_id));
            ns.post({
                action: 'dobusinesslogin',//dologin
                Username: that.form.username,
                Password: that.form.password,
                device_type: that.form.device_type,
                device_id: that.form.device_id,
                openid: that.form.openid,
                logintype: that.form.logintype
            }, function(succeed, data, err) {
                if (succeed) {
                    api.toast({
                        msg: '登录成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.setPrefs({
                        key: 'uid',
                        value: data.uid
                    });
                    api.setPrefs({
                        key: 'busid',
                        value: data.busid
                    });
                    // api.setPrefs({
                    //     key: 'familyuid',
                    //     value: data.familyuid
                    // });
                    // api.setPrefs({
                    //     key: 'isfirstchangepwd',
                    //     value: data.isfirstchangepwd ? 1 : 0
                    // });
                    api.closeToWin({
                        name: 'root'
                    });
                    api.sendEvent({
                        name: 'onlogin'
                    });
                    // setTimeout(function() {
                    //   api.closeWin();
                    // }, 1000);
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '登录中'
            });
        },
        get_jiguang_register_id: function() {
            var that = this;
            console.log("111");
            ajpush.getRegistrationId(function(res, err) {
                console.log(JSON.stringify(res));
                if (res && res.id) {
                    that.form.device_id = res.id;
                    api.setPrefs({
                        key: 'cid',
                        value: that.form.device_id || ''
                    });
                }
            });
        },
        do_retrieve: function() {
            var that = this;
            ns.openWin('retrievepwd_frm', '找回密码');
        },
        wx_login: function() {
            var that = this;
            wx.auth({}, function(ret, err) {
                if (ret.status) {
                    that.wx_gettoken(ret.code);
                }
            });
        },
        wx_gettoken: function(code) {
            var that = this;
            wx.getToken({
                code: code
            }, function(ret, err) {
                if (ret.status) {
                    that.wx_getuserinfo(ret.accessToken, ret.openId)
                }
            });
        },
        wx_getuserinfo: function(token, openid) {
            var that = this;
            wx.getUserInfo({
                accessToken: token,
                openId: openid
            }, function(ret, err) {
                if (ret.status) {
                    that.form.openid = ret.openid;
                    that.form.logintype = 8;
                    that.do_login();
                } else {
                    api.toast({
                        msg: err.code,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        qq_login: function() {
            var that = this;
            qq.login(function(ret, err) {
                if (ret && ret.openId) {
                    that.form.openid = ret.openId;
                    that.form.logintype = 9;
                    that.do_login();
                }
            });
        },
        weibo_login: function() {
            var that = this;
            weibo.isInstalled(
                function(ret) {
                    if (ret.status) {
                        weibo.auth(function(ret, err) {
                            if (ret.status) {
                                that.form.openid = ret.userId;
                                that.form.logintype = 10;
                                that.do_login();
                            }
                        });
                    } else {
                        api.toast({
                            msg: '未安装新浪微博客户端',
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }
            );
        }
    }
});
apiready = function() {
    api.parseTapmode();
    ns = window.Foresight.Util;
    ajpush = api.require('ajpush');
    // wx = api.require('wx');
    // qq = api.require('QQPlus');
    // weibo = api.require('weibo');
    toast = new auiToast();
    app.get_jiguang_register_id();
    app.form.device_type = api.systemType;
    setTimeout(function(){
      app.get_data();
    },500)
    app.get_thridlogin_auth();
    api.sendEvent({
        name: 'close_popup'
    })
};
